package com.nidu.demo.dict.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.dict.dto.DictTypeAddCmd;
import com.nidu.demo.dict.ability.DictTypeDomainService;
import com.nidu.demo.dict.convertor.DictTypeConvertor;
import com.nidu.demo.dict.dto.DictTypeCO;
import com.nidu.demo.dict.gateway.DictTypeGateway;
import com.nidu.demo.dict.model.DictType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 字典类型创建命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class DictTypeAddCmdExe {

    private final DictTypeGateway gateway;

    private final DictTypeDomainService domainService;

    private final DictTypeConvertor convertor;

    public SingleResponse<DictTypeCO> execute(DictTypeAddCmd cmd) {
        // 1. 转换为领域对象
        DictType dictType = DictType.create(cmd.getName(), cmd.getType(), cmd.getSortOrder());

        // 2. 检查对象是否存在
        domainService.validateDuplicate(dictType.getType(), null);

        // 3. 调用网关保存
        DictType save = gateway.create(dictType);

        // 4. 转换为DTO返回
        DictTypeCO dictTypeCO = convertor.toClientObject(save);

        return SingleResponse.of(dictTypeCO);
    }
} 